import {SAVE_MACHINE_LISTS, SAVE_MACHINE_WHOLE_TIME_STATUS, SAVE_MACHINE_WORK_STATUS, SAVE_NAVIGATION_INDEX} from "./constants";
import storageSession from "redux-persist/lib/storage/session";
import {persistReducer} from "redux-persist";
import {STATUS_NOT_WORK} from "../../../pages/main/run/config";

const persistConfig = {
  key: "user",
  storage: storageSession,
  whitelist: ["navigationIndex"] //  白名单:isUser会存入缓存，其他不会存
};

const defaultState = {
  machineLists: [],
  navigationIndex: 0,
  machineWholeTimeStatus: {},
  machineWorkStatus: STATUS_NOT_WORK
};

function reducer(state = defaultState, action) {
  switch (action.type) {
    case SAVE_MACHINE_LISTS:
      return {...state, machineLists: action.machineLists};
    case SAVE_NAVIGATION_INDEX:
      return {...state, navigationIndex: action.navigationIndex};
    case SAVE_MACHINE_WHOLE_TIME_STATUS:
      return {...state, machineWholeTimeStatus: action.machineWholeTimeStatus};
    case SAVE_MACHINE_WORK_STATUS:
      return {...state, machineWorkStatus: action.machineWorkStatus};
    default:
      return state;
  }
}

export default persistReducer(persistConfig, reducer);